/*
 * @Author: your name
 * @Date: 2022-03-15 10:56:06
 * @LastEditTime: 2022-03-15 15:31:50
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /visualNotes/src/service/users/index.ts
 */
import { SUCCESS_CODE, ERROR_CODE } from "@/service";
import { db, _c } from "..";

export interface UserDTO {
  UUID: string;
  userName?: string;
  email?: string;
  role?: string;
  avatarUrl?: string;
  gender?: string;
  number?:string;
}

export const addUser = (user: UserDTO) => {
  return db
    .collection("users")
    .add(user)
    .then((res) => {
      if (res) {
        return {
          code: SUCCESS_CODE,
          data: true,
          message: "",
        };
      } else {
        return {
          code: ERROR_CODE,
          data: false,
          message: "信息更新错误",
        };
      }
    })
    .catch((err) => ({
      code: ERROR_CODE,
      data: false,
      message: "信息更新错误",
    }));
};

export const updateUser = (user: UserDTO) => {
  return db
    .collection("users")
    .update(user)
    .then((res) => res);
};

export const getUserInfo = (UUID: string) => {
  return db
    .collection("users")
    .where({ UUID: _c.eq(UUID) })
    .get()
    .then(({ data }) => {
      if (data.length < 1) {
        return false;
      } else {
        return data;
      }
    });
};
